package com.iyao_app.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.SceneUser;
import com.iyao_app.util.ImageData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/11.
 */

public class PopImageAdapter extends BaseRecyclerAdapter<SceneUser, PopImageAdapter.ViewHolder> {

    public PopImageAdapter(Context context, List<SceneUser> data) {
        super(context, data);
    }

    @Override
    protected ViewHolder getViewHolder(int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_pop_image, null));
    }

    @Override
    protected void onBindViewHolder_(ViewHolder holder, final int position) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams((int) context.getResources().getDimension(R.dimen.x60), (int) context.getResources().getDimension(R.dimen.x60));
        if(position==0){
            params.setMargins(0, 0, 0, 0);
        }else {
            params.setMargins(-20, 0, 0, 0);
        }
        SceneUser sceneUser = data.get(position);
        holder.sdvPhoto.setLayoutParams(params);

        try {
            Uri uri = Uri.parse(sceneUser.avatarUrl);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions((int) context.getResources().getDimension(R.dimen.x60), (int) context.getResources().getDimension(R.dimen.x60)))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.sdvPhoto.getController())
                    .setImageRequest(request)
                    .build();
            holder.sdvPhoto.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.sdv_photo)
        SimpleDraweeView sdvPhoto;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    PictureSelectListener selectListener;

    public void setOnPictureSelectListener(PictureSelectListener onPictureSelectListener) {
        selectListener = onPictureSelectListener;
    }

    public interface PictureSelectListener {
        void onSelect(int position);
    }
}
